import { devUserLoginInfo } from "@/config/env-config";
import commonBaseURL from "@/config/request/base-url";
import { form, get } from "@/utils/network/index";
import { setChanganToken, getChanganToken } from "@/utils/base/token-util";
import qs from "qs";

// 获取长安资源中心token
export async function getRescenterToken(): Promise<any> {
  const params = devUserLoginInfo;
  const res = await form(
    "/rest/resRestApi/v2/userLogin",
    qs.stringify(params),
    {
      baseURL: commonBaseURL.rescenterUrl,
    }
  );
  console.log("----res----", res);
  if (res.data.isSuccess) {
    setChanganToken(res.data.msg);
    return res.data.msg;
  }
  return Promise.reject(new Error(res.msg));
}
export async function setRescenterTokenByHandle(): Promise<any> {
  const tempToken = localStorage.getItem("tempToken");
  setChanganToken(tempToken);
  return Promise.resolve(tempToken);
}
// 通过token获取长安用户信息
export async function getChanganUserByToken(): Promise<any> {
  const res = await get("/rest/resRestApi/v2/getUserByToken", {
    params: {
      identityToken: getChanganToken(),
    },
    baseURL: commonBaseURL.rescenterUrl,
  });

  return res;
}
